package thread2;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class completableFuture3 {
    public static void main(String[] args) {
        CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {

            try {
                System.out.println(Thread.currentThread().getName());
                TimeUnit.SECONDS.sleep(3);
                System.out.println("f1 executed");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 2;
        });
        CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println(Thread.currentThread().getName());
                TimeUnit.SECONDS.sleep(3);
                System.out.println("f2 executed");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 3;
        });

        CompletableFuture<Integer> f3 = f1.thenCombine(f2, (a, b) -> a * b);
        System.out.println(f3.join());

        CompletableFuture<Integer> f4 = CompletableFuture.supplyAsync(() -> 4);
        CompletableFuture<Integer> f5 = f1.thenCompose(a->{
            return CompletableFuture.supplyAsync(() -> a+5);
        });
        System.out.println(f5.join());
        System.out.println(f1.join());
        System.out.println(f1.join());
    }
}
